package com.lihepeng.leecode.sentinel;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;

import java.util.ArrayList;
import java.util.List;

/**
 * sentinel 相关练习  测试
 */
public class Example {
    public static void main(String[] args) {
        initFlowRules();
        Entry helloWorld = null;
        while  (true) {
            try {
                 helloWorld = SphU.entry("HelloWorld");
                System.out.println("helloWorld");

            } catch (BlockException e) {
                e.printStackTrace();
            }finally {
                helloWorld.close();
            }
        }
    }
    private static void initFlowRules(){
        List<FlowRule> rules = new ArrayList<>();
        FlowRule rule = new FlowRule();
        rule.setResource("HelloWorld");
        rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        // Set limit QPS to 20.
        rule.setCount(20);
        rules.add(rule);
        FlowRuleManager.loadRules(rules);
    }

}
